home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
MATH
/
RMATH
/
MATH.DOC
< prev
next >
Wrap
Text File
|
1990-08-17
|
21KB
|
575 lines
MATH -- a unit of elementary mathematical routines for Turbo
Pascal versions 4, 5, and 5.5.
Copyright 1990, by J. W. Rider
MATH.PAS provides a library of mathematical constants, type
definitions, functions, and procedures for immediate use within
contemporary Turbo Pascal programs (tested with version 5.5).
Routines are provided for the following categories:
Numerical rounding and fractioning:
ceil, floor, modf, remf, round2, sgn
Binary operators:
atan2, fmod, fdiv, frem, hypot, ipow, iroot,
max, min, pow, powi, root, round2
Conditional operators:
iif
Polynomial evaluation:
poly
Exponential and power-raising functions:
exp, exp2, exp10, ipow, iroot, pow, powi, pow10,
root, sqrt
Logarithmic functions:
ln, log, log2, log10
Circular trigonometric functions and inverses:
acos, acosd, asin, asind, atan, atan2, atan2d,
atand, cosd, sind, tan, tand
Degree/radian conversions for circular trig:
deg2dms, deg2rad, dms2deg, dms2rad, rad2deg,
rad2dms
Hyperbolic trigonometric functions and inverses:
acosh, asinh, atanh, cosh, sinh, tanh
Some caveats:
MATH.PAS redefines the following SYSTEM unit functions:
exp, ln, pi, sqrt
In order to improve performance, none of the MATH
routines perform any range-checking for invalid values.
Generally, this only means that the result returned will be
equally invalid. However, there are some special values that
WILL result in run-time errors for some of the routines. Calling
the MATH routines with illegal or improper inputs is strictly at
the programmer's risk.
While many of the MATH.PAS routines have parallels in
Turbo C, there are some exceptions where the behavior could be
astonishingly different. This has to do with how "MOD" should be
interpreted. In Turbo C, (and, in Turbo Pascal, as well) "MOD"
is considered to be what is left-over after integer division. In
MATH.PAS, "MOD" is considered to be a mathematical "modulus"
function. For what it is that is left over after division, the
example of the Ada programming language is followed with the use
of the term "REM" (for "remainder"). If you never try to use the
related functions with negative arguments, you'll never have to
worry about whether the difference is important.
MATH.PAS constant definitions
These are provided as a programmer convenience. The
routines in MATH.PAS use many, but not all, of them. However,
there is no particular need for the application program to refer
to them at all.
CBRT2 the cube root of 2
CBRT3 the cube root of 3
D2R the number of radians in one degree of arc
E base of natural logarithms
LN2 natural logarithm of 2
LN3 natural logarithm of 3
LN10 natural logarithm of 10
LOG2E 1/ln(2)
LOG10E 1/ln(10)
M2R the number of radians in one minute of arc
PI plain ol' round pi -- the ratio of the
circumference of a circle to its diameter. (Note:
this constant replaces the "pi" function of the
SYSTEM unit.)
PI_2 pi/2
PI_4 pi/4
R1_E 1/e
R1_PI 1/pi
R1_SQRTPI 1/sqrt(pi)
R2_PI 2/pi
R2_SQRTPI 2/sqrt(pi)
R2D number of degrees of arc in a radian
S2R the number of radians in one second of arc
SQRPI this is a square, vice round, pi
SQRT_2 sqrt(1/2)
SQRT2 sqrt(2)
SQRTPI sqrt(pi) or gamma(1/2)
MATH.PAS type definitions
Depending upon whether or not MATH.PAS is compiled to do
floating point manipulations in hardware or software, the
routines will return different values.
float = either "real" or "double" depending upon the
status of the N option.
xfloat = either "real" or "extended" depending upon the
status of the N option.
The "float" type is expected to be the primary storage
mechanism for application programs. The "xfloat" type is
provided to maximize precision in expressions. There are only a
few functions where it is important to know the difference. They
are:
deg2dms -- returns "float" values through the DMS
arguments
modf -- returns a "float" value through one of its
arguments
poly -- expects the polynomial coefficients to be an
array of "float"
rad2dms -- returns "float" values through the DMS
arguments
remf -- returns a "float" value through one of its
arguments
(Because of Turbo Pascal's strong type-checking, you may run into
an occasional problem where you've declared a "real" or "double"
variable -- or you're redefined "float" in another unit -- and
can't pass the variable to one of the above routines. In those
cases, you should be able to do variable type-casting --
"math.float(x)" to bypass the type-checking. This should be done
with care.)
For programmer convenience, MATH.PAS defines a structured type
called "floatray" which is the largest array that can hold
elements of type "float" (as defined above). There is normally
no need to refer to "floatray" in application programs.
MATH.PAS function and procedure definitions
name (parameter list) type of result
---- ---------------- --------------
acos (x: xfloat) xfloat
Return the principal value of the inverse circular cosine
of "x" for abs(x)<=1. The returned value is in radians
and ranges between 0 and PI. Related functions:
"acosd","asin", "atan", "system.cos".
acosd (x: xfloat) xfloat
Returns the principal value of the inverse circular
cosine of "x" for abs(x)<=1. The returned value is in
decimal degrees and ranges between 0 and 180. Related
functions: "acos", "cosd".
acosh (x: xfloat) xfloat
Returns the principal value of the inverse hyperbolic
cosine of "x" for x>=1. The returned value is greater
than or equal to 0. Related functions: "asinh", "atanh",
"cosh".
asin (x: xfloat) xfloat
Returns the principal value of the inverse circular sine
of "x" for abs(x)<=1. The returned value is in radians
and ranges between -PI/2 and +PI/2. Related functions:
"acos", "asind", "atan", "system.sin".
asind (x: xfloat) xfloat
Returns the principal value of the inverse circular sine
of "x" for abs(x)<=1. The returned value is in decimal
degrees and ranges between -90 and +90. Related
functions: "asin", "sind".
asinh (x: xfloat) xfloat
Returns the inverse hyperbolic sine of "x". Related
functions: "acosh", "atanh", "sinh".
atan (x: xfloat) xfloat
Returns the principal value of the inverse circular
tangent of "x". The returned value is in radians and
ranges between -PI/2 and +PI/2. Note: this is the same
as "arctan" in the SYSTEM unit. This function is
provided for nomenclature consistency only. Related
functions: "acos", "asin", "atan2", "tan".
atan2 (x, y: xfloat) xfloat
Returns the angle, in radians, between the x-axis and the
line segment connecting the origin and the coordinate
(x,y). This is the angular component generated in
converting from cartesian coordinates (x,y) to polar.
The range of ATAN2 is bounded between -PI and +PI
radians. Related functions: "atan", "hypot", "tan".
atan2d (x, y: xfloat) xfloat
Returns the angle, in decimal degrees, between the x-axis
and the line segment connecting the origin and the
coordinate (x,y). This is the angular component
generated in converting from cartesian coordinates (x,y)
to polar. The range of ATAN2D is bounded between -180 and
+180 degrees. Related functions: "atan2", "tand".
atand (x: xfloat) xfloat
Returns the principal value of the inverse circular
tangent of "x". The returned value is in decimal
degrees, and ranges between -90 and +90. Related
functions: "system.arctan", "tand".
atanh (x: xfloat) xfloat
Returns the inverse hyperbolic tangent of "x" for
abs(x)<1. Related functions: "acosh", "asinh", "tanh".
ceil (x: xfloat) xfloat
Returns the smallest integer value greater than or equal
to "x". (Rounds up.) Related functions: "floor" (rounds
down), "system.int" (rounds toward zero), "system.round"
(rounds to nearest integer), "round2" (rounds to
specified number of decimal places).
cosd (x: xfloat) xfloat
Returns the circular cosine of "x" degrees. COSD ranges
between -1 and +1. Related functions: "acosd",
"system.cos".
cosh (x: xfloat) xfloat
Returns the hyperbolic cosine of "x". The returned value
is greater than or equal to 0. Related functions:
"acosh", "sinh", "tanh".
deg2dms (deg: xfloat; var d,m,s :float) ------
Converts "deg" degrees into degree-minute-second
notation. Related functions: "deg2rad", "dms2deg".
deg2rad (x: xfloat) xfloat
Converts "x" degrees into radians. Related functions:
"deg2dms", "rad2deg".
dms2deg (d,m,s: xfloat) xfloat
Converts degrees-minute-seconds into decimals degrees.
Related functions: "deg2dms", "dms2rad".
dms2rad (d,m,s: xfloat) xfloat
Converts degrees-minute-seconds into decimals radians.
Related functions: "rad2dms", "dms2deg".
exp (x: xfloat) xfloat
Returns the value of "e" (the base of the natural
logarithms) raised to the "x" power. If x is
sufficiently negative, merely returns 0. Otherwise, it
calls "system.exp". Note: this function is provided as a
replacement for the "exp" of the SYSTEM unit which
overflows (!) when "x" is very negative. Related
functions: "system.exp", "exp2", "exp10", "math.ln",
"pow", "root".
exp2 (x: xfloat) xfloat
Returns the value of "2" raised to the "x" power.
Related functions: "math.exp", "exp10", "log2".
exp10 (x: xfloat) xfloat
Returns the value of "10" raised to the "x" power.
Related functions: "math.exp", "exp2", "log10", "pow10".
deg2rad (x:xfloat) xfloat
Converts "x" degrees into radians. Related functions:
"deg2dms", "rad2deg".
fdiv (x, y: xfloat) xfloat
Returns the number of times that "y" divides evenly into
"x" (rounds toward 0) -- a floating point analogue of the
integer "DIV" operator. Related functions: "fmod",
"frem", "remf".
floor (x: xfloat) xfloat
Returns the largest integer value less than or equal to
"x". (Rounds down.) Related functions: "ceil" (rounds
up), "system.int" (rounds toward zero), "system.round"
(rounds toward nearest integer), "round2" (rounds to
specified number of decimal places).
fmod (x, y: xfloat) xfloat
Returns the modulus that results from evenly dividing "y"
into "x" (rounded down). For constant "y", "fmod" is a
periodic function of "x". (This is NOT the same as Turbo
C's "fmod" nor Turbo Pascal's MOD operator for integers!)
Related functions: "frem", "modf".
frem (x, y: xfloat) xfloat
Returns the remainder that results after evenly dividing
"y" into "x" (rounded toward 0). For constant "y",
"frem" is an odd function of "x". A floating point
analogue of the integer MOD operator.) Related functions:
"fdiv", "fmod", "remf".
hypot (x, y: xfloat) xfloat
Returns the hypoteneuse of the right triangle with sides
"x" and "y". This is the range component generated in
converting cartesian coordinates (x,y) to polar
coordinates. Related functions: "atan2".
iif (p: boolean; t, f: xfloat) xfloat
Returns "t" if "p" is true; otherwise returns "f". Note:
this routine will force the unnecessary evaluation of
either "t" or "f" expressions without regard to which
value is returned. It works more efficiently if "t" and
"f" are variables vice expressions. This function is
used within several places in the source code where that
situation arises.
ipow (x, y: xfloat) xfloat
Returns the imaginary component of the complex value that
results when real "x" is raised the real "y" power. (Function
"pow" returns the real component.) There may be an
infinite number of satisfactory values, and this function
only returns one. Related functions: "iroot", "pow".
iroot (x, y: xfloat) xfloat
Returns the imaginary component of the complex value that
results when taking the real "y" root of the real "x"
value. (Function "root" returns the real component.)
There may be an infinite number of satisfactory values,
and this function only returns one. Related functions:
"ipow", "root".
ln (x: xfloat) xfloat
Returns the natural logarithm of "x". If x<0, it returns
the real component of the complex logarithm value. (The
imaginary component is "pi".) Note: a simpler version
of "ln" exists in the SYSTEM unit. Related functions:
"math.exp", "log", "log2", "log10".
log (x: xfloat) xfloat
Returns the natural logarithm of "x". Related functions:
"math.ln", "log2", "log10".
log2 (x: xfloat) xfloat
Returns the base two logarithm of "x". (Base two
logarithms are used extensively in information theory.)
Related functions: "exp2", "math.ln", "log", "log10".
log10 (x: xfloat) xfloat
Returns the common or base ten logarithm of "x". Related
functions: "exp10", "math.ln", "log", "log2", "pow10".
max (x, y: xfloat) xfloat
Returns either "x" or "y", whichever is greater. Related
functions: "min".
min (x, y: xfloat) xfloat
Returns either "x" or "y", whichever is less. Related
functions: "max".
modf (x: xfloat; var ipart: float) xfloat
Divides the value of "x" into an integer part and a
fractional part. The integer part (rounded down --
making it different from Turbo C's "modf" and Turbo
Pascal's integer MOD operator) is returned in "ipart".
The fractional part is return as the value of the
function. Related functions: "fmod", "remf".
poly (x: xfloat; degree: integer; var coeffs ) xfloat
Returns the evaluation of the polynomial
2 degree
coeffs[0]+coeffs[1]*X+coeffs[2]*X +...+coeffs[degree]*X
IMPORTANT! The untyped var "coeffs" is presumed to be an
array of "float", but the compiler does not enforce this
restriction. Passing any other structure to "poly" will
generate inexplicable results.
pow (x, y: xfloat) xfloat
Returns "x" raised the "y" power. Uses an exponential -
logarithm algorithm. Related functions: "math.exp",
"ipow", "powi", "root", "system.sqr".
pow10 (x: xfloat) xfloat
Returns ten raised to the "x" power. Same thing as
"exp10". This function is provided for certain
programmers who are accustomed to exponentiating "10"
with a function of this name.
powi (x: xfloat; n: integer) xfloat
Returns "x" raised to the "n" power. Uses a recursive
squaring - multiplication algorithm. Related functions:
"pow", "system.sqr".
rad2deg (x: xfloat) xfloat
Converts "x" radians into degrees. Related functions:
"deg2rad".
rad2dms (r:xfloat; var d,m,s: float) ------
Converts "r" radians into degrees-minute-seconds. Related
functions: "dms2rad", "rad2deg".
remf (x: xfloat; var ipart: float) xfloat
Divides the value of "x" into an integer part and a
fractional part. The integer part (rounded toward 0) is
returned in "ipart". The fractional part is return as
the value of the function. Related functions: "fdiv",
"frem", "modf". For an example of use, see the source
code for the procedure "deg2dms".
root (x, y: xfloat) xfloat
Returns "x" raised to the "y" power. If x<0, then the
real component of the complex power is returned. Related
functions: "pow", "system.sqrt".
round2 (x: xfloat; n: shortint) xfloat
Returns "x", rounded to "n" decimal places to the right
of the decimal point. If "n" is negative, rounding
occurs to the left of the decimal point. NOTE! Because
of the approximate nature of storing fractional
components in floating point representations, you should
be never assume that the result of "round2" is *exact*.
However, it should be very, very close.
sgn (x: xfloat) xfloat
Returns the "signum" of "x". If x>0, then "+1" is
returned. If x<0, "-1" is returned. If x=0, then "0"
is returned. Related functions: "system.abs".
sind (x: xfloat) xfloat
Returns the circular sine of "x" degrees. SIND ranges
from -1 to +1. Related functions: "asind", "system.sin".
sinh (x: xfloat) xfloat
Returns the hyperbolic sine of "x". Related functions:
"asinh", "cosh", "tanh".
sqrt (x: xfloat) xfloat
Returns the square root of "x". If x>=0, then
SYSTEM.SQRT is invoked. For x<0, this function returns
the negative of system.sqrt(-x). This makes "math.sqrt"
an odd function. Also, note that "math.sqrt(-1) <>
root(-1,2)". Instead, "math.sqrt(-1) = -iroot(-1,2)".
Related functions: "root", "system.sqr", "system.sqrt".
tan (x: xfloat) xfloat
Returns the circular tangent of "x". The value of "x" is
assumed to be expressed in radians. Related functions:
"system.sin", "system.cos", "system.arctan".
tand (x: xfloat) xfloat
Returns the circular tangent of "x" degrees. Related
functions: "atand", "tan".
tanh (x: xfloat) xfloat
Returns the hyperbolic tangent of "x". TANH ranges from
-1 to 1. Related functions: "atanh", "cosh", "sinh".